/*
 * @Author: 彭林
 * @Date: 2021-05-14 18:19:20
 * @LastEditTime: 2021-07-01 15:09:51
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /src/saas-app/app/api/v1/class.go
 */
package v1

import (
	"saas-app/app/define"
	"saas-app/app/model"
	"saas-app/app/service"
	"saas-app/library/response"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

func GetClassList(r *ghttp.Request) {

	campus_id := r.GetCtxVar("campus_id").Int()

	var (
		pagingParams *define.PagingParams
	)

	if err := r.Parse(&pagingParams); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result, total, err := service.ClassService().GetClassList(campus_id, pagingParams)

	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "ok", response.Paging(result, total, pagingParams.Page, pagingParams.Limit))
}

func GetClassSimpleList(r *ghttp.Request) {

	campus_id := r.GetQueryUint("school_id")

	result, err := service.ClassService().GetClassSimpleList(campus_id)

	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "ok", result)
}

func UpdateClass(r *ghttp.Request) {

	class_id := r.GetQueryUint("class_id")

	var data = make(map[string]interface{})

	if r.GetForm("class_name") != nil {
		data["class_name"] = r.GetFormString("class_name")
	}

	if r.GetForm("class_type") != nil {
		data["class_type"] = r.GetFormString("class_type")
	}

	if r.GetForm("capacity") != nil {
		data["capacity"] = r.GetFormUint("capacity")
	}

	if r.GetForm("remark") != nil {
		data["remark"] = r.GetFormString("remark")
	}

	if r.GetForm("status") != nil {
		data["status"] = r.GetFormUint("status")
	}

	result := service.ClassService().UpdateClass(class_id, data)

	response.JsonExit(r, 0, "ok", result)
}

func CreateClass(r *ghttp.Request) {

	var (
		data       *define.ClassRequest
		classParam *model.Classes
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if err := gconv.Struct(data, &classParam); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result := service.ClassService().CreateClass(classParam)

	response.JsonExit(r, 0, "ok", result)
}

func GetClassInfo(r *ghttp.Request) {
	class_id := r.GetQueryInt("class_id")
	result := service.ClassService().GetClassInfo(class_id)
	response.JsonExit(r, 0, "班级详情", result)
}
